package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import com.rla.domain.bo.BaseDrugSpeciBo;
import com.rla.domain.vo.BaseDrugSpeciVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.BaseDrugOnlineBo;
import com.rla.domain.vo.BaseDrugOnlineVo;
import com.rla.domain.BaseDrugOnline;
import com.rla.mapper.BaseDrugOnlineMapper;
import com.rla.service.IBaseDrugOnlineService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 线上商品（套餐、积分商品）共用Service业务层处理
 *
 * @author rla
 * @date 2025-08-04
 */
@RequiredArgsConstructor
@Service
public class BaseDrugOnlineServiceImpl implements IBaseDrugOnlineService {

    private final BaseDrugOnlineMapper baseMapper;

    private final BaseDrugSpeciServiceImpl baseSpeciService;

    /**
     * 查询线上商品（套餐、积分商品）共用
     */
    @Override
    public BaseDrugOnlineVo queryById(String id){
        BaseDrugOnlineVo vo = baseMapper.selectVoById(id);

        BaseDrugSpeciBo baseDrugSpeciBo = new BaseDrugSpeciBo();
        baseDrugSpeciBo.setDrugCode(vo.getDrugCode());
        List<BaseDrugSpeciVo> speciList = baseSpeciService.queryList(baseDrugSpeciBo);
        vo.setSpeciList(speciList);
        return vo;
    }

    @Override
    public BaseDrugOnline getInfoByDrugCode(String drugCode) {

        LambdaQueryWrapper<BaseDrugOnline> lqw = Wrappers.lambdaQuery();
        lqw.eq(BaseDrugOnline::getDrugCode,drugCode);
        BaseDrugOnline baseDrugOnline = baseMapper.selectOne(lqw);
        return baseDrugOnline;
    }

    /**
     * 查询线上商品（套餐、积分商品）共用列表
     */
    @Override
    public TableDataInfo<BaseDrugOnlineVo> queryPageList(BaseDrugOnlineBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseDrugOnline> lqw = buildQueryWrapper(bo);
        Page<BaseDrugOnlineVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询线上商品（套餐、积分商品）共用列表
     */
    @Override
    public List<BaseDrugOnlineVo> queryList(BaseDrugOnlineBo bo) {
        LambdaQueryWrapper<BaseDrugOnline> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseDrugOnline> buildQueryWrapper(BaseDrugOnlineBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseDrugOnline> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDrugCode()), BaseDrugOnline::getDrugCode, bo.getDrugCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugType()), BaseDrugOnline::getDrugType, bo.getDrugType());
        lqw.like(StringUtils.isNotBlank(bo.getDrugName()), BaseDrugOnline::getDrugName, bo.getDrugName());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), BaseDrugOnline::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getSliderImage()), BaseDrugOnline::getSliderImage, bo.getSliderImage());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoUrl()), BaseDrugOnline::getVideoUrl, bo.getVideoUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getProductInfo()), BaseDrugOnline::getProductInfo, bo.getProductInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getProductDetails()), BaseDrugOnline::getProductDetails, bo.getProductDetails());
        lqw.eq(bo.getSort() != null, BaseDrugOnline::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getSales()), BaseDrugOnline::getSales, bo.getSales());
        lqw.eq(StringUtils.isNotBlank(bo.getIsShow()), BaseDrugOnline::getIsShow, bo.getIsShow());
        lqw.eq(StringUtils.isNotBlank(bo.getIsRecommend()), BaseDrugOnline::getIsRecommend, bo.getIsRecommend());
        lqw.eq(StringUtils.isNotBlank(bo.getIsFreeShipping()), BaseDrugOnline::getIsFreeShipping, bo.getIsFreeShipping());
        lqw.eq(StringUtils.isNotBlank(bo.getIsCombination()), BaseDrugOnline::getIsCombination, bo.getIsCombination());
        lqw.eq(StringUtils.isNotBlank(bo.getIsTop()), BaseDrugOnline::getIsTop, bo.getIsTop());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsCategory()), BaseDrugOnline::getGoodsCategory, bo.getGoodsCategory());
        lqw.eq(bo.getShowTime() != null, BaseDrugOnline::getShowTime, bo.getShowTime());
        lqw.eq(StringUtils.isNotBlank(bo.getIsExpiration()), BaseDrugOnline::getIsExpiration, bo.getIsExpiration());
        lqw.eq(bo.getStartExpirationDate() != null, BaseDrugOnline::getStartExpirationDate, bo.getStartExpirationDate());
        lqw.eq(bo.getEndExpirationDate() != null, BaseDrugOnline::getEndExpirationDate, bo.getEndExpirationDate());
        lqw.eq(bo.getPackagePrice() != null, BaseDrugOnline::getPackagePrice, bo.getPackagePrice());
        lqw.eq(bo.getDashingPrice() != null, BaseDrugOnline::getDashingPrice, bo.getDashingPrice());
        return lqw;
    }

    /**
     * 新增线上商品（套餐、积分商品）共用
     */
    @Override
    public Boolean insertByBo(BaseDrugOnlineBo bo) {
        BaseDrugOnline add = BeanUtil.toBean(bo, BaseDrugOnline.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改线上商品（套餐、积分商品）共用
     */
    @Override
    public Boolean updateByBo(BaseDrugOnlineBo bo) {
        BaseDrugOnline update = BeanUtil.toBean(bo, BaseDrugOnline.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseDrugOnline entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除线上商品（套餐、积分商品）共用
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<BaseDrugOnlineVo> queryGoodsList(BaseDrugOnlineBo bo, PageQuery pageQuery) {
        QueryWrapper<BaseDrugOnline> lqw = Wrappers.query();
        lqw.eq(StringUtils.isNotBlank(bo.getDrugType()), "p.drug_type", bo.getDrugType());
        lqw.eq(StringUtils.isNotBlank(bo.getIsShow()), "p.is_show", bo.getIsShow());
        lqw.like(StringUtils.isNotBlank(bo.getIsRecommend()), "p.is_recommend", bo.getIsRecommend());
        lqw.eq(StringUtils.isNotBlank(bo.getId()), "t.id", bo.getId());
        lqw.eq("p.del_flag", "0");
        lqw.eq("p.is_combination", "0");//组合选品不展示
        lqw.and(StringUtils.isNotBlank(bo.getSearchValue()), w -> {
            w.like("p.drug_name", bo.getSearchValue()).or().like("p.py_code", bo.getSearchValue()).or().like("p.product_info", bo.getSearchValue()).or().like("s.speci_name", bo.getSearchValue());
        });
        lqw.and(StringUtils.isNotBlank(bo.getShopFlag())&& "normal".equals(bo.getShopFlag()), w -> {
            w.eq("p.goods_category", "P").or().eq("p.goods_category", "TC");
        });
        lqw.and(StringUtils.isNotBlank(bo.getShopFlag())&& "points".equals(bo.getShopFlag()), w -> {
            w.eq("p.goods_category", "JP");
        });

        Page<BaseDrugOnlineVo> result = baseMapper.selectDrugOnlineList(pageQuery.build(), lqw, bo);
        return TableDataInfo.build(result);
    }
}
